package com.leng.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.Quarter;
import cn.hutool.http.HttpUtil;
import com.leng.entity.User;
import com.leng.response.Result;
import com.leng.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @作者: 冷俊杰
 * @类名: EchartsController类
 * @创建时间: 2022/8/29 21:10
 * @描述: ECharts图表接口+动态全国疫情地图
 * @修改原图
 * @修改作者
 * @修改时间
 */
@RestController
@CrossOrigin
@Slf4j
@RequestMapping("/echart")
public class EchartsController {
    @Autowired
    private UserService userService;

    //图标演示数据
    @GetMapping("/example")
    public Result get() {
        Map<String, Object> map = new HashMap<>();
        map.put("x", CollUtil.newArrayList("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"));
        map.put("y", CollUtil.newArrayList(150, 230, 224, 218, 135, 147, 260));
        return Result.ok().data("map", map);
    }

    @GetMapping("/menbers")
    public Result menbers() {
        List<User> list = userService.list();
        int q1 = 0;//第1季度
        int q2 = 0;//第2季度
        int q3 = 0;//第3季度
        int q4 = 0;//第4季度
        for (User user : list) {
            Date createTime = user.getCreateTime();
            Quarter quarter = DateUtil.quarterEnum(createTime);
            switch (quarter) {
                case Q1:
                    q1 += 1;
                    break;
                case Q2:
                    q2 += 1;
                    break;
                case Q3:
                    q3 += 1;
                    break;
                case Q4:
                    q4 += 1;
                    break;
                default:
                    break;
            }
        }
        return Result.ok().data("map",CollUtil.newArrayList(q1,q2,q3,q4));
    }


    @GetMapping("/epidemic")
    public String epidemic() {
        log.info("-------------进入地图----------");
        return HttpUtil.get("https://c.m.163.com/ug/api/wuhan/app/data/list-total?t=" + System.currentTimeMillis());
    }

}